<?php
namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class School extends Base {

    public function info(){
        $schools = Db::table('active_school')->select()->toArray();
        View::assign([
            'schools' => $schools
        ]);
        return View::fetch();
    }


    public function add(){
        if(Request::isPost()){
            $school_name = trim(input('post.school_name'));
            $school_abbr = trim(input('post.school_abbr'));
            $status = intval(input('post.status'));
            if(empty($school_name)){
                return_json(1,'请输入学院名称');
            }
            if(empty($school_abbr)){
                return_json(1,'请输入学院简写');
            }
            $insert = Db::table('active_school')->insert([
                'school_name'=>$school_name,
                'school_abbr'=>$school_abbr,
                'status'=>$status
            ]);
            if(empty($insert)){
                return_json(1,'添加失败,请重试');
            }
            return_json(0,'添加成功');
        }else{
            return View::fetch();
        }
    }

    public function edit(){
        if(Request::isPost()){
            $id = input('post.id');
            $school_name = trim(input('post.school_name'));
            $school_abbr = trim(input('post.school_abbr'));
            $status = intval(input('post.status'));

            if(empty($school_name)){
                return_json(1,'请输入学院名称');
            }
            if(empty($school_abbr)){
                return_json(1,'请输入学院简写');
            }
            $insert = Db::table('active_school')->where('id',$id)->update([
                'school_name'=>$school_name,
                'school_abbr'=>$school_abbr,
                'status'=>$status
            ]);
            if(empty($insert)){
                return_json(1,'修改失败,请重试');
            }
            return_json(0,'修改成功');
        }else{
            $id = input('get.id');
            if(empty($id)){
                $this->error('请传入导航ID');
            }
            $school = Db::table('active_school')->where('id',$id)->find();
            View::assign([
                'school'=>$school
            ]);
            return View::fetch();
        }
    }

    public function del(){
        $id = input('post.id');
        if(empty($id)){
            return_json(1,'删除失败,请稍后再试');
        }
        $del = Db::table('active_school')->where('id',$id)->delete();
        if(empty($del)){
            return_json(1,'删除失败,请稍后再试');
        }else{
            return_json(0,'删除成功');
        }
    }

}